package com.hanyxx.io;

import java.io.FileWriter;
import java.io.IOException;

/**
 * @author layman
 * @date 2021/3/8
 */
public class Demo05 {
    private static FileWriter fw;
    private static char[] chars = new char[]{'这','把','我','必','C'};
    private static String str = "葫芦小金刚";
    public static void main(String[] args) throws IOException {
        //writeSingleChar();
        //writeCharArray(chars);
        //writeCharArray02(chars,1,3);
        writeString(str);
    }
    // 写单个字符 write(int c) 底层实际上调用的是write(new char[]{(char)c}, 0, 1);
    private static void writeSingleChar() throws IOException {
        // 如果文件不存在，会创建该文件
        fw = new FileWriter("layman01.txt");
        char a = '哈';
        fw.write(a);
        fw.flush();
        fw.close();
    }
    // 写字符数组
    private static void writeCharArray(char[] chars) throws IOException {
        fw = new FileWriter("layman01.txt");
        fw.write(chars);
        fw.flush();
        fw.close();
    }
    // 写字符数组的一部分
    private static void writeCharArray02(char[] chars,int offset,int length) throws IOException {
        fw = new FileWriter("layman01.txt");
        fw.write(chars,offset,length);
        fw.flush();
        fw.close();
    }
    //写字符串
    private static void writeString(String str) throws IOException {
        fw = new FileWriter("layman01.txt");
        fw.write(str);
        fw.flush();
        fw.close();
    }
}
